package com.itheima.config;


import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.itheima.utils.ThreadLocalUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.Map;

/**
 * @author sunyanhu
 */
@Slf4j
@Component
public class MybatisMetaObjectHandler implements MetaObjectHandler {


    @Override
    public void insertFill(MetaObject metaObject) {
        Map<String,Object> map = ThreadLocalUtil.get();
        log.info("start insert fill ....");
        metaObject.setValue("createTime", null);
        metaObject.setValue("createUser", null);
        this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
        this.strictInsertFill(metaObject, "deleted", Integer.class, 0);
        this.strictInsertFill(metaObject, "createUser", String.class,   map.get("username").toString());
    }

    @Override
    public void updateFill(MetaObject metaObject) {
        Map<String,Object> map = ThreadLocalUtil.get();
        log.info("start update fill ....");
        metaObject.setValue("updateTime", null);
        this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
        metaObject.setValue("updateUser", null);
        this.strictUpdateFill(metaObject, "updateUser", String.class, map.get("username").toString());
    }
}